package com.wk.springboot.jdk8.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @User admin
 * @Date 2020/11/18 9:03
 **/
public class StreamTest {

    public static void main(String[] args) {
        //filter过滤器
//        filter();
        //映射
//        map();
        //排序
//        sort();
        //匹配与查找
//        terminal();
        //reduce
//        reduce();
        collect();
    }

    private static void createStream(){
        //Collection 对象 创建 Stream
        Collection<String> list = new ArrayList<>();
        Stream<String> stream = list.stream();//创建串行流
        Stream<String> parallelStream = list.parallelStream();//创建并行流

        //Arrays创建流
        Stream<Object> arrayStream = Arrays.stream(Arrays.asList(1, 21, 3).toArray());

        Stream<Integer> integerStream = Stream.of(1, 3, 4, 5, 6);

        Stream<Integer> iterateStream = Stream.iterate(0, x -> x + 2);
    }

    private static void filter(){
        Stream<Integer> integerStream = Stream.of(1, 3, 4, 5, 6,7,9,9,9,9);
        integerStream.filter(x->x>3)//过滤流中大于3的元素 ,out:45679999
                .limit(6)//限制6条 ,out:456799
                .skip(1)//跳过第一个元素，out:56799
                .distinct()//根据元素的equals方法去重 ，out:5679
                .forEach(x->System.out.print(x));
    }

    private static void map(){
        Stream<Integer> integerStream = Stream.of(1, 3, 4, 5, 6,7,9,9,9,9);
        integerStream.map(x->x+2)//让没有元素都+2  out:35678911111111
        .forEach(x-> System.out.print(x));

        System.out.println("----------------");

        List<String> words = new ArrayList<String>();
        words.add("your");
        words.add("name");


        List<Stream<Character>> streamList = words.stream().map(w -> characterStream(w)).collect(Collectors.toList());
        streamList.stream().forEach(x-> System.out.println(x));

        List<Character> characterList = words.stream().flatMap(w -> characterStream(w)).collect(Collectors.toList());
        characterList.forEach(x-> System.out.println(x));
    }

    public static Stream<Character> characterStream(String s){
        List<Character> result = new ArrayList<>();
        for (char c : s.toCharArray())
            result.add(c);
        return result.stream();
    }

    private static void sort(){
        Stream<Integer> integerStream = Stream.of(1, 3, 4, 5, 6,7,9,8,9,9);
        integerStream.sorted() //自然排序，out:1345678999
                .forEach(x-> System.out.print(x));


        Stream<Integer> integerStream1 = Stream.of(1, 3, 4, 5, 6,7,9,8,2,9);
        integerStream1.sorted(Double::compare)
                .forEach(x-> System.out.print(x));//根据Double.compare排序，out:1234567899
    }

    private static void terminal(){
        Stream<Integer> integerStream1 = Stream.of(1, 3, 4, 5, 6,7,9,8,2,9);
        boolean b = integerStream1.allMatch(x->x>1);
        System.out.printf("b="+ b); //false

        Stream<Integer> integerStream = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        boolean b1 = integerStream.anyMatch(x -> x > 1);
        System.out.println("b1 = " + b1);//true

        Stream<Integer> integerStream2 = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        boolean b2 = integerStream2.noneMatch(x -> x > 1);
        System.out.println("b2 = " + b2);//false

        Stream<Integer> integerStream3 = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        Optional<Integer> first = integerStream3.findFirst();
        System.out.println("first.get() = " + first.get());//1

        Stream<Integer> integerStream4 = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        Optional<Integer> any = integerStream4.findAny();
        System.out.println("any.get() = " + any.get());//1

        Stream<Integer> integerStream5 = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        long count = integerStream5.count();
        System.out.println("count = " + count);//10

        Stream<Integer> integerStream6 = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        Optional<Integer> max = integerStream6.max((x, y) -> {
            if (x > y) {
                return 1;
            } else return -1;
        });
        System.out.println("max.get() = " + max.get());//根据传入的比较器，得到9
    }

    private static void reduce(){
        Stream<Integer> integerStream = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        Integer sum = integerStream.reduce(0, (x, y) -> x + y);
        System.out.println("sum = " + sum); //sum = 54
    }

    private static void collect(){
        Stream<Integer> integerStream = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        //把所有的元素排序放到一个list中
        List<Integer> integerList = integerStream
                .sorted()
                .collect(Collectors.toList());
        System.out.println("integerList = " + integerList);//integerList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 9]

        //求平均值
        Stream<Integer> integerStream1 = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        Double average = integerStream1.collect(Collectors.averagingDouble(x -> x));
        System.out.println("average = " + average); //average = 5.4

        //求个数
        Stream<Integer> integerStream2 = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        Long count = integerStream2.collect(Collectors.counting());
        System.out.println("count = " + count);//count = 10

        //按照流是否大于6来分组
        Stream<Integer> integerStream3 = Stream.of(1, 3, 4, 5, 6, 7, 9, 8, 2, 9);
        Map<Boolean, List<Integer>> collect = integerStream3.collect(Collectors.groupingBy(x -> x > 6));
        System.out.println("collect = " + collect);//collect = {false=[1, 3, 4, 5, 6, 2], true=[7, 9, 8, 9]}

    }

}
